package cn.doitedu.rtdw.dashboard;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class JOB03_每5分钟每类页面uv最大的前10个页面 {

    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(5000);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);

        // 建表，映射 kafka中 dwd层 行为日志明细宽表
        tenv.executeSql(
                " create table dwd_events_kafka (                       "+
                        "     release_channel string                             "+
                        "     ,device_type string                                 "+
                        "     ,session_id  string                                 "+
                        "     ,lat         double                                 "+
                        "     ,lng         double                                 "+
                        "     ,username    string                                 "+
                        "     ,event_id     string                                "+
                        "     ,action_time   bigint                               "+
                        "     ,properties  map<string,string>                     "+
                        "     ,user_id  bigint                                    "+
                        " 	  ,member_level_id bigint                             "+
                        "     ,password string                                    "+
                        "     ,nickname string                                    "+
                        "     ,phone string                                       "+
                        "     ,status int                                         "+
                        "     ,create_time timestamp(3)                           "+
                        "     ,icon string                                        "+
                        "     ,gender int                                         "+
                        "     ,birthday date                                      "+
                        "     ,register_city string                               "+
                        "     ,job string                                         "+
                        "     ,personalized_signature string                      "+
                        "     ,source_type int                                    "+
                        "     ,integration int                                    "+
                        "     ,growth int                                         "+
                        "     ,lucky_count int                                    "+
                        "     ,history_integration int                            "+
                        "     ,modify_time timestamp(3)                           "+
                        "     ,province string                                    "+
                        "     ,city string                                        "+
                        "     ,region string                                      "+
                        "     ,page_type string                                   "+
                        "     ,page_service string                                "+
                        "     ,page_lanmu string                                  "+
                        "     ,page_pindao string                                 "+
                        "     ,pt AS proctime()                                   "+   // 处理时间语义
                        "     ,rt AS to_timestamp_ltz(action_time,3)              "+
                        "     ,watermark for rt as rt - interval '0' second       "+   // 事件时间语义
                        " ) with (                                                "+
                        "     'connector' = 'kafka',                              "+
                        "     'topic' = 'dwd-events',                             "+
                        "     'properties.bootstrap.servers' = 'doitedu:9092',    "+
                        "     'properties.group.id' = 'goo2',                     "+
                        "     'scan.startup.mode' = 'latest-offset',              "+
                        "     'value.format'='json',                              "+
                        "     'value.json.fail-on-missing-field'='false',         "+
                        "     'value.fields-include' = 'EXCEPT_KEY'               "+
                        " )                                                       "
        );


        // 每5分钟，每类 页面中访问人数最多的前10个页面及其访问人数
        tenv.executeSql(
                " WITH tmp AS (                                                                                      "+
                        "     SELECT                                                                                          "+
                        "         window_start,                                                                               "+
                        "     	window_end,                                                                                   "+
                        "     	page_type,                                                                                    "+
                        "     	properties['url'] as page_url,                                                                "+
                        "     	count(distinct user_id) as uv                                                                 "+
                        "     FROM TABLE(                                                                                     "+
                        "        TUMBLE(TABLE dwd_events_kafka,DESCRIPTOR(rt),INTERVAL '5' MINUTE)                            "+
                        "     )                                                                                               "+
                        "     GROUP BY                                                                                        "+
                        "         window_start,                                                                               "+
                        "     	window_end,                                                                                   "+
                        "     	page_type,                                                                                    "+
                        "     	properties['url']                                                                             "+
                        " )	                                                                                                  "+
                        "                                                                                                     "+
                        " SELECT                                                                                              "+
                        "     window_start,                                                                                   "+
                        "     window_end,                                                                                     "+
                        "     page_type,                                                                                      "+
                        "     page_url,                                                                                       "+
                        "     uv,                                                                                             "+
                        "     rn                                                                                              "+
                        " FROM                                                                                                "+
                        " (                                                                                                   "+
                        "    SELECT                                                                                           "+
                        "           window_start,                                                                             "+
                        "        	window_end,                                                                               "+
                        "        	page_type,                                                                                "+
                        "        	page_url,                                                                                 "+
                        "        	uv,                                                                                       "+
                        "    		row_number() over(PARTITION by window_start,window_end,page_type ORDER BY uv DESC) as rn  "+
                        "    FROM tmp                                                                                         "+
                        " ) o                                                                                                 "+
                        " WHERE rn<=10                                                                                        "
        ).print();




    }
}
